
class UsersController < ApplicationController

  before_action :require_no_logined, :only => [:new]
  before_action :require_logined, :only => [:edit, :update, :modify_password]
  before_action :is_self?, :only => [:edit, :update, :modify_password]
  before_action :set_user, :only => [:edit, :update, :modify_password]
  before_action :find_user, :only => [:show]

  layout 'auth', only: [:new, :create]

  def new
    @user = User.new :password => '1' # force client side validation patch
    referrer = request.headers['X-XHR-Referer'] || request.referrer
    store_location referrer if referrer.present?

    set_seo_meta("注册阳毅门窗官方商城")

    if mobile?
      render :action => :mobile_new
    end
  end

  def create
    if current_user && params[:is_designer]
      current_user.update_attributes params[:user].permit!
      current_user.register_designer
      redirect_to dashboard_designers_path
    else
      @user = User.new user_params
      if mobile?
        @user.on_mobile = true
        @user.term_of_service = true
      end
      @user.in_password_reset = true
      @user.require_sms_code = true #unless Rails.env.development?
      @user.valid_sms_code = SmsService.get_valid_sms_code(@user)
      #@user.need_captcha = true
      if @user.save
        login_as @user
        if params[:is_designer]
          @user.register_designer
          redirect_to dashboard_designers_path
        else
          unless mobile?
            flash[:success] = "注册成功，欢迎您#{@user.nickname}！"
            redirect_to root_path
          else
            redirect_to root_path
          end
        end
      else
        if @user.errors && @user.errors.messages && @user.errors.messages[:sms_code]
          flash[:error] = @user.errors.messages[:sms_code].first
        #else
          #flash[:error] = '注册发生错误，请检查必填信息是否完整'
        end

        if params[:is_designer]
          render :controller => :designers, :action => :new
        else
          unless mobile?
            render :action => :new
          else
            render :action => :mobile_new, :layout => false
          end
        end
      end
    end
  end

  def edit
    set_seo_meta("基本资料_个人中心")
    render :layout => 'user'
  end

  def update
    u_params = reset_user_region(@user, user_params)
    if @user.update_attributes(u_params)
      flash[:success] = "更新用户资料成功"
    else
      flash[:error] = @user.errors.messages.values.join("\n")
    end
    redirect_to :back
  end

  def modify_password
    @user.in_password_reset = true

    set_seo_meta("重置密码_个人中心")
    render :layout => 'user'
  end

  private

  def reset_user_region user, u_params
    if u_params[:province_id].nil? || u_params[:city_id].nil?
      u_params.delete :province_id
      u_params.delete :city_id
    end
    u_params
  end

  def is_self?
    authorize! :update, current_user
  end

  def set_user
    @user = current_user
  end

  def user_params
    params.require(:user).permit(:name, :nickname, :mobile, :description, :sms_code, :sys_invite_code, :sys_invite_token, :email, :password, :password_confirmation, :avatar, :province_id, :city_id, :birthday, :position).delete_if { |key, value| key =~ /(password|birthday|province_id|city_id)/ && value.empty? }
  end

  def find_user
    # 处理 login 有大写字母的情况
    if params[:id] != params[:id].downcase
      redirect_to request.path.downcase, status: 301
      return
    end

    @user = User.find_by_nickname(params[:id])
    if @user.blank?
      render404
    end
  end


end

